package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.commom.response.JsonResult;
import cn.tedu._04weibo.commom.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMApper;
import cn.tedu._04weibo.pojo.dto.CommentDTO;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.vo.CommentIndexVO;
import cn.tedu._04weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Api(tags = "3评论模块")
@RestController
@RequestMapping("v1/comment/")
public class CommentContorller {
    @Autowired(required = false)
    private CommentMApper commentMApper;

    /**
     * 发布评论功能
     * @return
     */
    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){//未登录
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //插入数据
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMApper.insert(comment);
        return JsonResult.ok();
    }
    //获取评论
    @ApiImplicitParam(name = "id" ,value = "评论编号",required = true,dataType = "int")
    //当参数为多个时可以使用ApiImplicitParams
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name ="id"，value ="微博编号"，required= true, dataType = "int")，
//            @ApiImplicitParam(name = "username"，value = “用户名"required = true， dataType = "string")
    @ApiOperation("获取评论")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(int id){
        List<CommentIndexVO> commentIndexVOS=commentMApper.selectByWeiboId(id);
        return JsonResult.ok(commentIndexVOS);
    }
}
